package com.coocaa.ops.admin.v1.panel.convert;

import com.coocaa.ops.admin.base.constant.BusinessTypeConstants;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.panel.validator.PanelValidator;
import com.coocaa.ops.common.core.base.constant.ContentTypeConstants;
import com.coocaa.ops.common.core.base.constant.CoverTypeConstants;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.panel.constant.BlockTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.entity.*;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.common.tool.exception.BusinessException;
import com.coocaa.ops.common.tool.string.StringUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/16 18:33
 */

@UtilityClass
public class PanelCommonSaveConverter {

    public static List<BlockExtendInfoEntity> getFinalBlockExtendInfoEntityList(List<BlockContentInfoEntity> blockContentInfoEntityList) {
        List<BlockExtendInfoEntity> totalBlockExtendInfoEntityList = InstanceUtil.newArrayList();
        for (BlockContentInfoEntity blockContentInfoEntity : blockContentInfoEntityList) {
            if (StringUtils.isNotBlank(blockContentInfoEntity.getIconTextPositionsJsonStr())) {
                BlockExtendInfoEntity blockExtendInfoEntity = new BlockExtendInfoEntity();
                blockExtendInfoEntity.setId(blockContentInfoEntity.getId());
                blockExtendInfoEntity.setIconTextPositionsJsonStr(blockContentInfoEntity.getIconTextPositionsJsonStr());
                blockExtendInfoEntity.setBgColor(blockContentInfoEntity.getBgColor());
                totalBlockExtendInfoEntityList.add(blockExtendInfoEntity);
            }
        }
        return totalBlockExtendInfoEntityList;
    }

    public static void handleSetDefaultValue(PanelInfoEntity inputEntity) {
        if (PanelTypeConstants.isMark(inputEntity.getPanelType())) {
            inputEntity.setBusinessType(BusinessTypeConstants.COMMON);
        }
        if (PanelTypeConstants.isGroup(inputEntity.getPanelType())) {
            handleGroupPanelDmpFlag(inputEntity);
        } else {
            handlePanelDmpFlag(inputEntity);
        }
    }

    /**
     * 获取要保存的推荐位信息
     *
     * @param blockList
     * @return
     */
    public static List<BlockContentInfoEntity> getFinalBlockList(Integer status, List<VirtuallyBlockInfo> blockList) {
        List<BlockContentInfoEntity> result = InstanceUtil.newArrayList();
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            handleEachVirtuallyBlockInfo(status, result, virtuallyBlockInfo);
        }
        return result;
    }

    /**
     * 封装推荐位信息-每一推荐位
     *
     * @param result
     * @param virtuallyBlockInfo
     */
    public static void handleEachVirtuallyBlockInfo(Integer status, List<BlockContentInfoEntity> result, VirtuallyBlockInfo virtuallyBlockInfo) {
        List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
        for (BlockContentConfigInfo blockContentConfigInfo : commonBlockList) {
            BlockContentInfoEntity saveEntity = PanelCommonSaveConverter.getBlockContentInfoEntity(status, blockContentConfigInfo);
            result.add(saveEntity);
        }
        List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
        if (dmpBlockList == null) {
            return;
        }
        for (BlockContentConfigInfo dmpBlock : dmpBlockList) {
            BlockContentInfoEntity saveEntity = PanelCommonSaveConverter.getBlockContentInfoEntity(status, dmpBlock);
            result.add(saveEntity);
        }
    }

    /**
     * @param status
     * @param blockContentConfigInfo
     * @return
     */
    static BlockContentInfoEntity getBlockContentInfoEntity(Integer status, BlockContentConfigInfo blockContentConfigInfo) {
        GlobalPictureEntity poster = blockContentConfigInfo.getGlobalPicture();
        if (BlockTypeConstants.NORMAL.equals(blockContentConfigInfo.getBlockType())) {
            Assert.notNull(poster, "poster info cannot not be empty");
            Assert.notNull(poster.getId(), "poster id cannot not be empty");
        }
        blockContentConfigInfo.setPosterId(poster != null ? poster.getId() : -1);
        //设置推荐位信息
        handleSetBlockInfoAll(status, blockContentConfigInfo);
        return blockContentConfigInfo;
    }


    /**
     * 设置推荐位信息-全部
     *
     * @param blockContentConfigInfo
     */
    private static void handleSetBlockInfoAll(Integer status, BlockContentConfigInfo blockContentConfigInfo) {
        //设置剧集等信息
        handleSeriesCheck(blockContentConfigInfo);
        //设置点击事件
        setOnClick(status, blockContentConfigInfo);
    }


    /**
     * 校验前段传入媒资的类别、剧集、评分、期号、更新状态是否符合格式
     *
     * @param videoContent
     */
    private static void handleSeriesCheck(BlockContentInfoEntity videoContent) {
        if (CoverTypeConstants.CUSTOM.equals(videoContent.getCoverType())) {
            videoContent.setVideoContentType(ContentTypeConstants.CUSTOM);
        }
        if (StringUtils.isBlank(videoContent.getPublishStatus())) {
            videoContent.setPublishStatus("unknown");
        }
        if (videoContent.getSeries() == null) {
            videoContent.setSeries(0);
        }
        if (videoContent.getCategoryId() == null) {
            videoContent.setCategoryId(0);
        }
        if (videoContent.getShowScore() == null) {
            videoContent.setShowScore(1);
        }
        if (videoContent.getShowSeries() == null) {
            videoContent.setShowSeries(1);
        }
    }

    /**
     * 设置 点击事件
     *
     * @param contentEntity
     */
    public static void setOnClick(Integer status, BlockContentConfigInfo contentEntity) throws BusinessException {
        if (BlockTypeConstants.PLUGIN.equals(contentEntity.getBlockType())) {
            //插件推荐位onclick需要设置默认值，否则数据库插入报json格式错误
            contentEntity.setOnclick("{}");
            return;
        }
        String onclick = contentEntity.getOnclick();
        //获取最终的点击事件
        contentEntity.setOnclick(StringUtil.replaceCharEnterAndTab(onclick));
        if (ResourceStatusValidator.isToAuditing(status)) {
            PanelValidator.checkOnclick(contentEntity.getOnclick());
        }
    }


    private void handlePanelDmpFlag(PanelInfoEntity panel) {
        panel.setHasDmp(0);

        List<VirtuallyBlockInfo> vBlockList = panel.getBlockList();
        if (CollectionUtils.isEmpty(vBlockList)) {
            return;
        }
        for (VirtuallyBlockInfo vBlock : vBlockList) {
            List<BlockContentConfigInfo> dmpBlockList = vBlock.getDmpBlockList();
            if (CollectionUtils.isNotEmpty(dmpBlockList)) {
                panel.setHasDmp(1);
                return;
            }
        }
    }

    private void handleGroupPanelDmpFlag(PanelInfoEntity panel) {
        panel.setHasDmp(0);

        List<SubPanelConfigInfo> subPanelList = panel.getSubPanelList();
        if (CollectionUtils.isEmpty(subPanelList)) {
            return;
        }
        for (SubPanelConfigInfo subPanel : subPanelList) {
            List<VirtuallyBlockInfo> vBlockList = subPanel.getBlockList();
            if (CollectionUtils.isEmpty(vBlockList)) {
                continue;
            }
            for (VirtuallyBlockInfo vBlock : vBlockList) {
                List<BlockContentConfigInfo> dmpBlockList = vBlock.getDmpBlockList();
                if (CollectionUtils.isNotEmpty(dmpBlockList)) {
                    panel.setHasDmp(1);
                    return;
                }
            }
        }
    }
}
